package com.qfedu.entity.course03;

/**
 *
 */
public class LinkedStack<E> {
    SNode<E> top;
    int size;

    public LinkedStack() {
        size = 0;
    }

    public int push(E e) {
        //创建新节点
        SNode<E> node = new SNode<>();
        node.data = e;
        //新节点的next指针指向栈顶元素
        node.next = top;
        //栈顶指针指向新的节点
        top = node;
        return 1;
    }

    public E pop() {
        if (top == null) {
            //空栈
            return null;
        }
        E e = top.data;
        //栈顶指针指向当前栈顶的next节点
        top = top.next;
        return e;
    }

    public E get() {
        if (top == null) {
            //空栈
            return null;
        }
        //取栈顶元素
        return top.data;
    }

    public boolean isEmpty() {
        return top == null;
    }

    public static class SNode<E> {
        E data;
        SNode next;
    }
}
